/**
 *
 *  @auth xlx_good@qq.com
 *  @date 17/6/19.
 *
 */

/**
 * 基础功能封装
 * @author xianglx
 * 20170616
 * */
(function(win, doc, undefined){

    /**
     *
     * 加载页面
     * @param <string> 加载页面地址
     * @param <function> 回调方法 加载页面
     *
     * */
    var loadPage = function(url, callback){
        var xmlHttp;
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlHttp=new XMLHttpRequest();
        }else{// code for IE6, IE5
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
                callback(xmlHttp.responseText);
            }
            if(xmlHttp.status == 404){
                console.log('invalid url!');
            }
        }
        xmlHttp.open("GET", url, true);
        xmlHttp.send();
    }
    /**
     * 动态添加页面，加载页面中的 script
     * @param <Array>  scripts 数组
     * @param <function> 回调方法
     * */
    var loadPageScript = function(scripts, callback){
        if(scripts.length ==0){
            return;
        }
        var script = doc.createElement('script');
        if(scripts[0].getAttribute('src')){
            script.setAttribute('src', scripts[0].getAttribute('src'));
            script.onload = function () {
                scripts[0].remove();
                loadPageScript(scripts, callback)
            }
        }else{
            script.innerHTML = scripts[0].innerHTML;
            scripts[0].remove();
            loadPageScript(scripts, callback)
        }
        doc.body.appendChild(script);
    }


    /**
     *  cookie
     *  @auth xlx_good@qq.com
     *  @date 17/5/17.
     *
     */
    var cookie = {
        set: function(name, value, opts) {
            if (!opts)
                opts = {};
            var cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value);
            if (opts.maxAge)
                cookie += '; Max-Age=' + opts.maxAge;
            if (opts.expires)
                cookie += '; Expires=' + (opts.expires.constructor == Date ? opts.expires.toUTCString() : new Date(opts.expires).toUTCString());
            if (opts.path)
                cookie += '; Path=' + opts.path;
            if (opts.domain)
                cookie += '; Domain=' + opts.domain;
            if (opts.secure)
                cookie += '; Secure';
            document.cookie = cookie;
        },

        get: function(name) {
            var result = document.cookie.match(new RegExp('(?:^|; )' + encodeURIComponent(name).replace(/[.*()]/g, '\\$&') + '=([^;]*)'));
            return result ? decodeURIComponent(result[1]) : null;
        },

        remove: function(name, opts) {
            if (!opts)
                opts = {};
            opts.maxAge = 0;
            cookie.set(name, '', opts);
        },
        removeAll: function(){
            var cookies = document.cookie.split(';')
            for(var i=0;i<cookies.length;i++){
                var key = cookies[i].split('=')[0].replace(/\s/g, '');
                cookie.remove(key);
            }
        }
    };

    win.Cookie = cookie;
    win.loadPageScript = loadPageScript;
    win.loadPage = loadPage;

})(window, document);